package myky.com.doctorapp.ui

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.CacheDiskStaticUtils
import com.gyf.barlibrary.ImmersionBar
import kotlinx.android.synthetic.main.activity_setting.*
import kotlinx.android.synthetic.main.public_common_title.*
import myky.com.doctorapp.R
import myky.com.doctorapp.api.Address
import myky.com.doctorapp.base.BaseActivity
import myky.com.doctorapp.base.CMApplication
import myky.com.doctorapp.contract.SettingActivityContract
import myky.com.doctorapp.entity.LogoutEntity
import myky.com.doctorapp.presenter.SettingActivityPresenter
import myky.com.doctorapp.utils.Constants
import myky.com.doctorapp.utils.DataCleanManager
import myky.com.doctorapp.utils.SystemUtil
import myky.com.doctorapp.utils.ToastUtils

/**
 * Description: 个人中心设置
 * @author wuyang
 * Created by 2019/1/24 14:46
 */
class SettingActivity : BaseActivity<SettingActivityPresenter, LogoutEntity.PayLoadBean>(), SettingActivityContract.View, View.OnClickListener {


    override fun getPresenter(): SettingActivityPresenter {
        return SettingActivityPresenter()
    }

    override fun reloadActivity() {
    }

    override fun injectTarget(): View = findViewById(R.id.setting_lin)

    override fun initData() {
    }

    override fun initView() {
        ImmersionBar.with(this).barColorInt(resources.getColor(R.color.colorPrimary)).titleBar(R.id.conmonLayout).navigationBarEnable(false).init()

        conmonTitleTextView.text = getText(R.string.Setting_sz)
        setting_logout.setOnClickListener(this)
        user_agreement_protro.setOnClickListener(this)
        privacy_user_protoco.setOnClickListener(this)
        cleanrCache.setOnClickListener(this)
        changgePwd.setOnClickListener(this)
        backLayout.setOnClickListener(this)


        if (!CMApplication.getInstance().isLoginOk) {
            setting_logout.visibility = View.GONE
        }

        val cacheSize = CacheDiskStaticUtils.getCacheSize()

        val formatSize = DataCleanManager.getFormatSize(cacheSize.toDouble())



        if (cacheSize == 0L) {

            cache_size.text = "0.0KB"
        } else {

            cache_size.text = formatSize

        }

        version_code.text=SystemUtil.getVersionName(this)
        

    }

    override fun getContenView(): Int = R.layout.activity_setting

    override fun showDataSuccess(datas: LogoutEntity.PayLoadBean?) {

        var intent = Intent()

        intent.putExtra("IS_LOGOUT", "IS_LOGOUT")

        setResult(Activity.RESULT_OK, intent)

        finish()
    }

    override fun onClick(view: View?) {

        when (view?.id) {

            R.id.setting_logout -> mPresenter.plogout()

            R.id.user_agreement_protro -> {


                var bundle = Bundle()
                bundle.putString(Constants.WEBURL, Address.USER_SERVICE_URL)
                bundle.putString(Constants.TITLE_H5, resources.getString(R.string.Setting_user))

                startActivity(WebProtocolActivity::class.java, bundle)

            }

            R.id.privacy_user_protoco -> {


                var bundle = Bundle()
                bundle.putString(Constants.WEBURL, Address.USER_SECREAT_URL)
                bundle.putString(Constants.TITLE_H5, resources.getString(R.string.Setting_xy))

                startActivity(WebProtocolActivity::class.java, bundle)

            }
            R.id.cleanrCache -> {

                val clear = CacheDiskStaticUtils.clear()

                if (clear) {
                    ToastUtils.show(resources.getString(R.string.cleancachsucces))
                }
            }
            R.id.changgePwd->{


                val intent=Intent(this,ChangePasswordActivity::class.java)
                startActivityForResult(intent,1)
            }
            R.id.backLayout->{
                finish()
            }

        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if(requestCode==1&&resultCode==Activity.RESULT_OK){

            var intent = Intent()

            intent.putExtra("IS_LOGOUT", "IS_LOGOUT")

            setResult(Activity.RESULT_OK, intent)

            finish()
        }
    }
}
